package adminService

import (
	"fmt"
	"gin-gorm-oj/define"
	"gin-gorm-oj/models"
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/copier"
	"net/http"
	"strconv"
)

type CategoryDTO struct {
	Id    int    `json:"id"    structs:"id"`
	Name  string `json:"name"  structs:"name"`
	Sort  int    `json:"sort"  structs:"sort"`
	Typec int    `json:"type"  structs:"type"`
}

func SaveCategory(c *gin.Context) {
	var categoryDTO CategoryDTO
	if err := c.ShouldBindJSON(&categoryDTO); err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}
	category := new(models.Category)
	copier.Copy(&category, &categoryDTO)
	category.Status = define.DEFAULT_STATUS_ENABLE
	if err := models.Db.Create(&category).Error; err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "服务器错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
}

func CategoryPageList(c *gin.Context) {
	name := c.Query("name")
	typec := c.Query("type")
	page, err := strconv.Atoi(c.DefaultQuery("page", define.DefaultPage))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", define.DefaultSize))
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}
	page = (page - 1) * pageSize
	var total int64
	var categories []models.Category
	db := models.Db.Model(new(models.Category))
	if name != "" {
		db = db.Where("name like ?", "%"+name+"%")
	}
	if typec != "" {
		typec, _ := strconv.Atoi(typec)
		db = db.Where("type = ?", typec)
	}
	err = db.Count(&total).Offset(page).Limit(pageSize).Find(&categories).Order("sort asc").Order("update_time desc").Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "查询失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"data": gin.H{
			"total":   total,
			"records": categories,
		},
	})

}

func UpdateCategory(c *gin.Context) {
	var categoryDTO CategoryDTO
	if err := c.ShouldBindJSON(&categoryDTO); err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}
	if categoryDTO.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}
	dbCategory := new(models.Category)
	err := models.Db.Model(new(models.Category)).Where("id = ?", categoryDTO.Id).First(&dbCategory).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}
	category := new(models.Category)
	copier.Copy(&category, &categoryDTO)
	if err := models.Db.Model(new(models.Category)).Where("id = ?", categoryDTO.Id).Updates(&category).Error; err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "更新失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "Update Success",
	})

}

func UpdateCategoryStatus(c *gin.Context) {
	status := c.Param("status")
	id, err := strconv.Atoi(c.Query("id"))
	fmt.Println("id", id)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}
	dbCategory := new(models.Category)
	err = models.Db.Model(new(models.Category)).Where("id = ?", id).First(&dbCategory).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}
	err = models.Db.Model(new(models.Category)).Where("id = ?", id).Update("status", status).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "更新失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "Update Status Success",
	})

}

func GetListByType(c *gin.Context) {
	typec, err := strconv.Atoi(c.Query("type"))
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}
	var categories []models.Category
	err = models.Db.Model(new(models.Category)).Where("type = ?", typec).Find(&categories).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "查询失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"data": categories,
	})

}

func DelCategory(c *gin.Context) {
	id, err := strconv.Atoi(c.Query("id"))
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}

	var cntSetmeal int64
	models.Db.Model(new(models.Setmeal)).Where("category_id = ?", id).Count(&cntSetmeal)
	if cntSetmeal > 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "该分类下有套餐，不能删除",
		})
		return
	}
	var cntDish int64
	models.Db.Model(new(models.Dish)).Where("category_id = ?", id).Count(&cntDish)
	if cntDish > 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "该分类下有菜品，不能删除",
		})
		return
	}
	err = models.Db.Where("id = ?", id).Delete(new(models.Category)).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "删除失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "Delete Success",
	})

}
